package com.cww.study.mybatis;

import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

/**
 * @program: myes
 * @description:
 * @author: 陈英俊
 * @create: 2021-09-16 14:24
 */
public class BaseServiceImpl<T> implements BaseService<T> {

    // 日志
    Logger log = LoggerFactory.getLogger(BaseServiceImpl.class);

    @Autowired
    public BaseMapper<T> mapper;

    private final Class<T> modelClass;


    public BaseServiceImpl() {
        /**
         * getGenericSuperclass 返回带泛型的超类
         * getSuperclass 返回父类
         *
         * 获取到接口的泛型,设置到实现类的modelClass属性里去
         */
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        this.modelClass = (Class) pt.getActualTypeArguments()[0];
    }

    public T save(T t) {
        this.mapper.insert(t);
        return t;
    }

    public T findByPrimaryKey(Serializable id) {
        return this.mapper.selectByPrimaryKey(id);
    }

    public Integer updateByPrimaryKeySelective(T t) {
        return this.mapper.updateByPrimaryKeySelective(t);
    }

    public Integer updateByPrimaryKey(T t) {
        return this.mapper.updateByPrimaryKey(t);
    }

    public Integer deleteByPrimaryKey(Serializable id) {
        return this.mapper.deleteByPrimaryKey(id);
    }

    public PageResultBean<T> findListByPage(Object example, PageSizeQueryDto dto) {
        this.log.info("调用通用查询分页");
        PageHelper.startPage(dto.getPageNum() == null ? 1 : dto.getPageNum(), dto.getPageSize() == null ? 10 : dto.getPageSize(), true);
        return new PageResultBean(this.mapper.selectByExample(example));
    }

    public List<T> findAll() {
        return this.mapper.selectAll();
    }

    public List<T> findListByCondition(Object condition) {
        return this.mapper.selectByExample(condition);
    }

    public T findOneByCondition(Object condition) {
        List list = this.mapper.selectByExample(condition);
        return list.size() > 0 ? (T) list.get(0) : null; // (T) 额外添加
    }

    public T findByCondition(T t) {
        return this.mapper.selectOne(t);
    }

    public Integer saveOrUpdate(T t) {
        int res = this.mapper.updateByPrimaryKey(t);
        return res <= 0 ? this.mapper.insert(t) : res;
    }

    public Integer saveOrUpdateBySelective(T t) {
        int res = this.mapper.updateByPrimaryKeySelective(t);
        return res <= 0 ? this.mapper.insert(t) : res;
    }

    public Integer batchSave(List<T> list) {
        return this.mapper.insertList(list);
    }

    public Integer batchSave(List<T> list, int batchSize) {
        int batch = list.size() / batchSize;
        int modelNum = list.size() % batchSize;
        int size = list.size();
        int count = 0;

        for (int index = 0; index < batch; ++index) {
            count = this.mapper.insertList(list.subList(index * batchSize, (index + 1) * batchSize));
        }

        if (modelNum > 0) {
            this.mapper.insertList(list.subList(batch * batchSize, size));
        }

        return count;
    }
}
